package com.vutil.src.dao;

import java.util.List;
import java.util.logging.Logger;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.util.vmsg.beans.MobileNumber;
import com.util.vmsg.beans.Person;
import com.util.vmsg.beans.Vehicle;

public class VMsgDAOImpl extends VMsgDAO {

	private DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	private static final Logger log = Logger.getLogger(VMsgDAOImpl.class.getName());

	@Override
	public boolean isExistingMobileNumber(MobileNumber mobileNumber) {
		try {
			Key mobileNumberKey = KeyFactory.createKey("MobileNumber",mobileNumber.getMobileNumber());
			Entity mobileNumberEntity = datastore.get(mobileNumberKey);
			if(mobileNumberEntity!=null)
				return true;			
		} catch (EntityNotFoundException e) {
			log.info("Mobile Number not created so far ==> " + mobileNumber.getMobileNumber());
			//e.printStackTrace();
		} 
		return false;
	}

	@Override
	public boolean isExistingVehicleNumber(Vehicle vehicle) {
		Key vehicleKey = KeyFactory.createKey("Vehicle",vehicle.getVehicleNumber());
		try {
			Entity vehicleEntity = datastore.get(vehicleKey);
			if(vehicleEntity!=null)
				return true;			
		} catch (EntityNotFoundException e) {
			log.info("Vehicle Number not created so far ==> " + vehicle.getVehicleNumber());
			//e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean checkDupeSubscription(Vehicle argVehicle,
			MobileNumber argMobileNumber) {
		Key vehicleKey = KeyFactory.createKey("Vehicle",argVehicle.getVehicleNumber());
		try {
			Entity vehicleEntity = datastore.get(vehicleKey);
			if(vehicleEntity!=null)
			{
				Vehicle vehicle = Vehicle.getVehicleVO(vehicleEntity);
				List<MobileNumber> vMsgSubscribers = vehicle.getvMsgSubscribers();
				for(MobileNumber vMsgSubscriber : vMsgSubscribers)
				{
					if(vMsgSubscriber.getMobileNumber().equalsIgnoreCase(argMobileNumber.getMobileNumber()))
						return true;
				}
			}
		} catch (EntityNotFoundException e) {
			log.info("Mobile Number not created so far ==> " + argMobileNumber.getMobileNumber());
			//e.printStackTrace();
		}
		return false;
	}
	
	@Override
	public List<MobileNumber> getVehicleNumberSubscriptionList(Vehicle vehicle) {
		List<MobileNumber> vMsgSubscribers = null;
		Key vehicleKey = KeyFactory.createKey("Vehicle",vehicle.getVehicleNumber());
		
		try {
			Entity vehicleEntity = datastore.get(vehicleKey);
			if(vehicleEntity!=null)
			{	
				vehicle = Vehicle.getVehicleVO(vehicleEntity);
				vMsgSubscribers = vehicle.getvMsgSubscribers();
				return vMsgSubscribers;
			}
		} catch (EntityNotFoundException e) {
			log.info("Vehicle Number not created so far ==> " + vehicle.getVehicleNumber());
		}
		return null;
	}

	@Override
	public boolean updateVMsgSubscriberList(Vehicle argVehicle,
			MobileNumber argMobileNumber) {
		Key vehicleKey = KeyFactory.createKey("Vehicle",argVehicle.getVehicleNumber());
		try {
			Entity vehicleEntity = datastore.get(vehicleKey);
			if(vehicleEntity!=null)
			{
				int vMsgSubscriberListSize = ((Long) vehicleEntity.getProperty("vMsgSubscriberListSize")).intValue();
				vehicleEntity.setProperty("vMsgSubscriberListSize",++vMsgSubscriberListSize);
				vehicleEntity.setProperty("vMsgSubscriber["+vMsgSubscriberListSize+"]", argMobileNumber.getMobileNumber());
				datastore.put(vehicleEntity);
				return true;
			}
		} catch (EntityNotFoundException e) {
			log.info("Mobile Number not created so far ==> " + argMobileNumber.getMobileNumber());
			//e.printStackTrace();
		}
		return false;
	}


	@Override
	public List<Person> getVMsgSubscribers(Vehicle vehicle) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Vehicle> getOwnedVehicles(Person vehicleOwner) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean create(Vehicle vehicle) {
		Entity vehicleEntity = vehicle.getVehicleEntity();
		datastore.put(vehicleEntity);
		return true;
	}

	@Override
	public boolean create(MobileNumber mobileNumber) {
		Entity mobileNumberEntity = mobileNumber.getMobileNumberEntity();
		datastore.put(mobileNumberEntity);
		return true;
	}

}
